package com.joeyzh.base;

import android.content.Context;

import com.joey.ui.widget.EmptyView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

/**
 * Created by Joey on 2018/11/22.
 * 列表控制的Presenter
 *
 * @author Joey
 */

public abstract class ListPresenter<VM extends ListViewModel> extends IPresenter<VM> {

    // 默认页码
    public static final int DEFAULT_PAGE = 1;
    // 默认一页多少数据
    public static final int DEFAULT_ROWS = 10;
    protected int page;
    protected EmptyView emptyView;

    public ListPresenter(Context context, VM vm) {
        super(context, vm);
    }

    public ListPresenter setEmptyLayout(EmptyView emptyView) {
        this.emptyView = emptyView;
        return this;
    }

    /**
     * 上拉加载
     */
    public void onLoadMore(RefreshLayout refreshLayout) {
        refreshLayout.finishLoadmore();
        page++;
        onLoad(page, DEFAULT_ROWS);

    }

    /**
     * 下拉刷新
     */
    public void onRefresh(RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        page = DEFAULT_PAGE;
        onLoad(page, DEFAULT_ROWS);

    }

    /**
     * 数据为空时更新数据
     */
    protected abstract void onEmpty();

    /**
     * 加载数据，这里写加载数据的方法
     *
     * @param page 页码
     * @param rows 行数
     */
    public abstract void onLoad(int page, int rows);

}
